<?php
//占位
class class_wss
{
	use trait_wss1;
	
	var $server = null;
	//---------------------------------------------------------------------------------------
	function onStart($ws)
	{
		$this->init($ws);
	}
	//---------------------------------------------------------------------------------------
	function onWorkerStart($ws, $cid)
	{
		$this->init($ws);
	}
	//---------------------------------------------------------------------------------------
	function onConnect($ws, $cid)
	{
		$this->init($ws);
	}
	//---------------------------------------------------------------------------------------
	function onOpen($ws, $request)
	{
		$this->init($ws);
		$this->send('swoole version:'.SWOOLE_VERSION);
	}
	//---------------------------------------------------------------------------------------
	function onMessage($ws, $cid, $str)
	{
		$this->init($ws);
		
		$GLOBALS['CID'] = $cid;
		$GLOBALS['FUNC'] = '';
		
		if($str{0} === '{')
		{
			$array = jsond($str);
			if(!is_array($array))
			{
				$this->err('json fail');
				return;
			}

			$params = $array['data'];
			$func = $params['func'];
			
			if(empty($func))
			{
				$this->err('empty func');
				return;
			}
			
			if($func != 'login')
			{
				$token = $array['token'];
				if(!isMd5($token))
				{
					$this->err('token format err');
					return;
				}
				
				if(!ww_sesscheck($token))
				{
					$this->err('token check fail');
					return;
				}
			}
			
			$default = [
				'login' => 1,
				'setpwd' => 1,
				'heart' => 1,
				'reload' => 1,
				'shutdown' => 1,
			];
			
			$GLOBALS['FUNC'] = $func;
			
			if(isset($default[$func]))
			{
				$func = "ww_{$func}";
			}
			else
			{
				$func = "wss_{$func}";
				if(!function_exists($func))
				{
					$this->err('no exists func');
					return;
				}
			}
			
			unset($params['func']);
			
			$GLOBALS['POOL']->on();
			$tmp = $func($this, $params);
			$GLOBALS['POOL']->off();
			
			if(empty($tmp))
			{
				$tmp = ww_format('');
			}
			
			$this->send($tmp);
		}
	}
	//---------------------------------------------------------------------------------------
	function onClose($ws)
	{		
		$this->init($ws);
	}
	//---------------------------------------------------------------------------------------
	function onShutdown($ws)
	{
		$this->init($ws);
	}
}